#include <QDebug>
#include <QTextCodec>

#include <gumbo.h>

#include "modelpageparser.h"
#include "imagepageparser.h"
#include "constants.h"
#include "utils.h"

namespace Jcr
{

class ModelPageParserPrivate
{
public:
    QString modelName;
    QString brandName;

    int marketPrice;
    int manufacturePrice;

    QStringList imageLinks;
    QStringList videoLinks;

    bool gotPrice = false;

    void getPrice(GumboNode* node);
    void getImageLinks(GumboNode* node);
    void getVideoLinks(GumboNode* node);
};

void ModelPageParserPrivate::getPrice(GumboNode *node)
{
    if (gotPrice)
        return;

    GumboAttribute* attr = nullptr;
    if (node->v.element.tag == GUMBO_TAG_DIV &&
       (attr = gumbo_get_attribute(&node->v.element.attributes, "class")) &&
       (strcasecmp(attr->value, "line_box ucar_box") == 0) ) {
        GumboVector* children = &node->v.element.children;
        for (unsigned int i = 0; i < children->length; ++i) {
            GumboNode* subNode = static_cast<GumboNode*>(children->data[i]);
            if (subNode->v.element.tag == GUMBO_TAG_DIV) {
                GumboVector* divChildren = &subNode->v.element.children;
                for (unsigned int divIndex = 0; divIndex < divChildren->length; ++divIndex) {
                    GumboNode* divNode = static_cast<GumboNode*>(divChildren->data[divIndex]);
                    std::string strData = Jcr::GumboUtils::getNodeText(divNode);
                    if (divNode->v.element.tag == GUMBO_TAG_FONT && divIndex == 2) {
                        marketPrice = atoi(strData.data());
                        gotPrice = true;
                    }
                }
            }
        }
    }
}

void ModelPageParserPrivate::getImageLinks(GumboNode *node)
{
    if (node->v.element.tag != GUMBO_TAG_UL)
        return;

    GumboAttribute* attr = gumbo_get_attribute(&node->v.element.attributes, "class");
    if (attr) {
        QString strClassName(attr->value);
        if (strClassName.compare("list_pic", Qt::CaseInsensitive) == 0) {
            std::list<std::string> links;
            Jcr::GumboUtils::getAttributeValue(node, GUMBO_TAG_A, "href", links);
            foreach (auto link, links) {
                if (Jcr::GumboUtils::isImageLink(link))
                    imageLinks.append(QString(link.c_str()));
            }
        }
    }
}

void ModelPageParserPrivate::getVideoLinks(GumboNode *node)
{
    if (node->v.element.tag != GUMBO_TAG_UL)
        return;

    GumboAttribute* attr = gumbo_get_attribute(&node->v.element.attributes, "class");
    if (attr) {
        QString strClassName(attr->value);
        if (strClassName.compare("list_pic", Qt::CaseInsensitive) == 0) {
            std::list<std::string> links;
            Jcr::GumboUtils::getAttributeValue(node, GUMBO_TAG_A, "href", links);
            foreach (auto link, links) {
                if (Jcr::GumboUtils::isVideoLink(link))
                    videoLinks.append(QString(link.c_str()));
            }
        }
    }
}

ModelPageParser::ModelPageParser(QObject* parent) : AbstractPageParser(parent)
{
    d_ptr = new ModelPageParserPrivate;
//    setUrl(QUrl(Jcr::TESTMODELPAGE2));
}

ModelPageParser::~ModelPageParser()
{
    delete d_ptr;
}

void ModelPageParser::setModelName(const QString& model)
{
    d_ptr->modelName = model;
}

QString ModelPageParser::modelName() const
{
    return d_ptr->modelName;
}

void ModelPageParser::processNode(GumboNode* node)
{
    // to do
    d_ptr->getPrice(node);
    d_ptr->getImageLinks(node);
    d_ptr->getVideoLinks(node);
}

void ModelPageParser::finalize()
{
//    qDebug() << "price is : " << d_ptr->marketPrice;
    qDebug() << "image links: " << d_ptr->imageLinks;
//    qDebug() << "video links: " << d_ptr->videoLinks;
//    foreach (auto imgLink, d_ptr->imageLinks) {
//        ImagePageParser* ipp = new ImagePageParser;
//        ipp->setModelName(this->modelName());
//        ipp->setBrandName(this->brandName());
//        ipp->setUrl(QUrl(imgLink));
//        ipp->run();
//    }

    emit finished();
}

void ModelPageParser::setBrandName(const QString& brand)
{
    d_ptr->brandName = brand;
}

QString ModelPageParser::brandName() const
{
    return d_ptr->brandName;
}

QStringList ModelPageParser::imageLinks() const
{
    return d_ptr->imageLinks;
}
QStringList ModelPageParser::videoLinks() const
{
    return d_ptr->videoLinks;
}


} // namespace jcr
